package com.mazaiting.instructions.math;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * 取反指令
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class NegInstruction {

    /**
     * 双精度浮点 取反
     */
    public static class DNeg extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            double value = stack.popDouble();
            stack.pushDouble(-value);
        }
    }

    /**
     * 单精度浮点 取反
     */
    public static class FNeg extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            float value = stack.popFloat();
            stack.pushFloat(-value);
        }
    }

    /**
     * 整型 取反
     */
    public static class INeg extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            int value = stack.popInt();
            stack.pushInt(-value);
        }
    }

    /**
     * 长整型 取反
     */
    public static class LNeg extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long value = stack.popLong();
            stack.pushLong(-value);
        }
    }
}
